/**
 * 双色球
 */


(function(){
	var url = 'http://www.17500.cn/getData/ssq.TXT';//目标网址
	var oli = document.querySelectorAll('header > .search > ul > li'),//搜索列表
		length = oli.length;
	var btn = document.querySelector('header > .search > .btn');//查询按钮
	var _box = document.querySelector('section > ul');
	var sArr = [];//要查询的数组
	var flag = false;//开关
	var css = document.getElementById('css-sheet');
 	var btnRest = document.getElementById('o-rest');
 	var btnFlesh = document.getElementById('o-reflesh');
 	var years = document.querySelectorAll('header > .search > nav > input');
 	var _key = true;//键盘事件节流
 	var _someFlag = false;
 	var btnSome = document.getElementById('o-some');

/*----------------------------------------------------------------------------*/
// 启动
	//saveFile(url,url);
	ajax();
	years[0].focus();
/*----------------------------------------------------------------------------*/
//定义

	//渲染
	function change(flagArr){
		var silders = document.querySelectorAll('section > ul > li'),
			length = silders.length;
		if(!flagArr.length){
			alert('共0注!没有查询到数据.');
		}

		for (var i = 0; i < length; i++) {
			silders[i].classList.add('dis-n');
			// silders[i].classList.remove('on');
		}

		flagArr.forEach(function(val,index){
			silders[val].classList.toggle('dis-n'); 
			// if(index === flagArr.length-1){
			// 	silders[val].classList.toggle('on');
			// }
			if(index === flagArr.length-1){
				alert(`共${flagArr.length}注`);
			}
		});

	}
	//创建li
	function createLi(arr){
		var length = arr.length;
		var frag = document.createDocumentFragment();
		var str = '';

		for (var i = 0; i < length; i++) {
			var obj = document.createElement('li');
			// if(i === length-1){
			// 	obj.classList.add('on');
			// }
			str = `
				<span>${arr[i][0]}</span>
				<span>${arr[i][1]}</span>
				<span>${arr[i][2]}</span>
				<span>${arr[i][3]}</span>
				<span>${arr[i][4]}</span>
				<span>${arr[i][5]}</span>
				<span>${arr[i][6]}</span>
				<span>${arr[i][7]}</span>
				<span>${arr[i][8]}</span>
			`;
			obj.innerHTML = str;
			str = '';
			frag.appendChild(obj);
		}
		_box.innerHTML = '';
		_box.appendChild(frag);
	}



	//匹配复合要求的数组
	function search(arr,info,time){
		var flagArr = [];
		var lastArr = [];
		var length = info.length;

		//查询年份月份
		if(time[0] || time[1]){
			for(var i=0,length = info.length;i<length;i++){
				var _arr = info[i][1].split('-');
				if(time[0] && time[1]){
					time[0] === +_arr[0] && time[1] === +_arr[1]?flagArr.push(i):'';
				}else{
					if(time[0]){
						time[0] === +_arr[0]?flagArr.push(i):'';
					}
					if(time[1]){
						time[1] === +_arr[1]?flagArr.push(i):'';
					}
				}
			}
		}
		if(!arr.length){
			change(flagArr);
			return;
		}
		for (var i = 0, length = info.length; i < length; i++) {
			if(!!flagArr.length){
				if(flagArr.includes(i)){
					doSe();
				}	
			}else {
				doSe();
			}
		}

		function doSe(){
			if(_someFlag){
				someNum();
			}else {
				sNum();
			}
		}
		function someNum(){
			var flag = false;
			var newArr = info[i].slice(2,8);
			for (var j = 0; j <arr.length; j++) {
				var str = (arr[j].toString()).padStart(2,"0");
				flag = newArr.includes(str);
				if(flag === false){
					break;
				}								
			}
			//console.log(newArr);
			flag ? lastArr.push(i):'';	
		}

		//完全匹配查询
		function sNum(){
			var flag = false;
			for (var j = 2; j <= 7; j++) {
				if(j-2<arr.length){
					flag = arr[j-2] === +info[i][j];
					if(flag === false){
						break;
					}
				}				
			}
			flag ? lastArr.push(i):'';	
		}
		//console.log(lastArr.length,flagArr.length);
		_someFlag = false;
		change(lastArr);
	}

	function init(info){
		// _box.classList.add('clearfix');
		createLi(info);


		for(var i=0;i<years.length;i++){
			years[i].index = i;
			years[i].onfocus = function(){
				var self = this;
				this.value = '';
				this.onblur = function(){
					var txt = +this.value;
					if(txt){
						if(!!this.index){
							if(txt<1 || txt>12){
								alert('请输入正确的月份!');
								self.focus();
							}
						}else {
							console.log(txt);
							if(txt<2003 || txt>3000){
								alert('请输入正确的年份!');
								self.focus();
							}
						}
					}
				};
			};
		}

		btnSome.onclick = function(){
			_someFlag = true;
			btn.click();
		};

		btnRest.onclick = function(){
			for (var i = 0; i < length; i++) {
				oli[i].innerText = '';
			}
			years[0].focus();
		};

		btnFlesh.onclick = function(){
			window.location.reload();
		};
		
		document.onkeydown = function(e = window.event){
			if(e.keyCode === 13){
				setTimeout(function(){
					_key = true;
				},200);
				if(_key){
					btn.click();
					_key = false;
					years[0].focus();
				}
			}
		};

		btn.onclick = function(){
			var arr = [];
			var yearsArr = [];
			var _flag = false;//查询红球开关;

			for(var i=0;i<years.length;i++){
				var time = +years[i].value;
				yearsArr.push(time);
			}

			//获取要搜寻的数字
			for(var i=0;i<length;i++){
				var number = +oli[i].innerText;
				if(!!number){
					arr.push(number);
				}else {
					for(;i+1<length;i++){
						oli[i+1].innerText = '';
					}
					break;
				}
			}
			
			for (var i = 0; i < arr.length; i++) {
				if(arr[i]>=arr[i+1]){
					alert('输入正确的数据!');
					for(;i+1<length;i++){
						oli[i+1].innerText = '';
					}
					break;
				}else {
					_flag = true;
					break;
				}
			}

			if(yearsArr[0] || yearsArr[1] || _flag){
				search(arr,info,yearsArr);

			}else {
				alert('请输入要查询的号码!');
			}
			//console.log(yearsArr[0] || yearsArr[1] || _flag);
		};

		//判断每数字是否在1-33之间
		for (var i = 0; i < length; i++) {
			oli[i].index = i;
			oli[i].onfocus = function(){
				var self = this;
				this.innerText = '';
				this.onblur = function(){
					if(!!oli[this.index].innerHTML){
						var number = oli[this.index].innerText*1;
						if(number && number>0 && number<34){
							
						}else {
							this.innerText = '';
							self.focus();
							alert('请输入正确的数字!');
						} 
					}
				};
			};
		}


	}
		
	//下载数据
	function saveFile(url,txt){
		var saveLink = document.createElement('a');
		saveLink.href = url;
		saveLink.download = txt;
		saveLink.innerText = '下载数据';
		//saveLink.click();
		document.body.appendChild(saveLink);
	}
	//访问本地数据
	function ajax(){
		var xhr = new XMLHttpRequest();
		xhr.open('get', 'sources/ssq.TXT', true);
		xhr.send();
		xhr.onreadystatechange = function(){
			if(xhr.readyState === 4){
				var dataString = xhr.responseText;	
				run(dataString);
			}
		};
	}
	//处理数据
	function run(data){
		var arr = data.split(/\b\s\b/);
		var info = formatData(arr);
		init(info);
	}
	//数据格式化
	function formatData(arr){
		var length = arr.length,
			fragArr = [],
			dataArr = [];

		arr.forEach(function(val,i){
			if( i !== 0 && i%29 === 0){
				dataArr.push(fragArr);
				fragArr = [val];
			}else if( i === length-1 ){
				fragArr.push(val);
				dataArr.push(fragArr);
			}else {
				fragArr.push(val);
			}
		});
		return dataArr;
	}
})();